<template>
  <div class="app-container">
    <!--查询表单-->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="courseDto.courseName" placeholder="课程名"/>
      </el-form-item>

      <!-- 一级分类 -->
      <el-select v-model="courseDto.departmentName" placeholder="一级学院" @change="oneLevelChanged">
        <el-option
          v-for="department in oneLevelList"
          :key="department.departmentCode"
          :label="department.departmentName"
          :value="department.departmentName"
        />
        <!-- key是唯一表标识，label是显示名，value是值 -->
      </el-select>
      <!-- 二级分类 -->
      <el-select v-model="courseDto.professionalName" placeholder="二级专业">
        <el-option
          v-for="twoLevel in twoLevelList"
          :key="twoLevel.professionalCode"
          :label="twoLevel.professionalName"
          :value="twoLevel.professionalName"
        />
      </el-select>

      <el-button type="primary" icon="el-icon-search" @click="getList()">查询</el-button>
      <el-button type="default" @click="resetData()">清空</el-button>
      <el-button type="success" plain @click="saveClear">新增课程</el-button>
    </el-form>

    <el-table :data="list" border fit highlight-current-row style="text-align:center">
      <el-table-column label="序号" width="70" align="center">
        <template slot-scope="scope">{{ (page - 1) * limit + scope.$index + 1 }}</template>
      </el-table-column>

      <el-table-column prop="courseName" label="课程名" align="center"/>

      <el-table-column prop="classGrade" label="年级" align="center"/>

      <el-table-column prop="professionalName" label="所属专业" align="center"/>

      <el-table-column prop="tecName" label="授课教师" align="center"/>

      <el-table-column prop="courseWeek" label="授课周" align="center"/>

      <el-table-column prop="courseTime" label="授课时间" align="center"/>

      <el-table-column prop="roomAddr" label="授课地址" align="center"/>

      <el-table-column label="操作" width="300" align="center">
        <template slot-scope="scope">
          <!-- <router-link :to="'/student/edit/'+scope.row.id">
            <el-button type="primary" size="mini" icon="el-icon-edit">修改</el-button>
          </router-link>-->
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-edit"
            @click="getCourseById(scope.row.id)"
          >修改</el-button>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="removeDataById(scope.row.id)"
          >删除</el-button>

          <el-button
            type="warning"
            size="mini"
            icon="el-icon-star-off"
            @click="saveOrUpdateCourse(scope.row.id,scope.row.professionalName,scope.row.tecName)"
            plain
          >课程安排</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- -------------------------添加或修改课程信息弹框---------------------------------- -->

    <el-dialog title="课程信息" :visible.sync="dialogFormVisible" :append-to-body="true" width="350px">
      <el-form :model="item">
        <el-form-item label="课程名" :label-width="formLabelWidth">
          <el-input v-model="item.courseName" autocomplete="off" style="width:300px"></el-input>
        </el-form-item>

        <el-form-item label="年级" :label-width="formLabelWidth">
          <el-input-number v-model="item.classGrade" :min="1900" :max="2100" label="年级"></el-input-number>
        </el-form-item>

        <el-select v-model="item.departmentName" placeholder="所属一级学院" @change="oneLevelChanged">
          <el-option
            v-for="department in oneLevelList"
            :key="department.departmentCode"
            :label="department.departmentName"
            :value="department.departmentName"
          />
          <!-- key是唯一表标识，label是显示名，value是值 -->
        </el-select>
        <!-- 二级分类 -->
        <el-select v-model="item.professionalName" placeholder="所属二级专业">
          <!-- @change="twoLevelChanged" -->
          <el-option
            v-for="twoLevel in twoLevelList"
            :key="twoLevel.professionalCode"
            :label="twoLevel.professionalName"
            :value="twoLevel.professionalName"
          />
        </el-select>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveOrUpdateCourseInfo">确 定</el-button>
      </div>
    </el-dialog>

    <!-- -------------------------添加或修改学院信息弹框---------------------------------- -->

    <!-- -------------------------课程安排---------------------------------- -->

    <el-dialog title="课程安排" :visible.sync="dialogForm" :append-to-body="true" width="350px">
      <el-form :model="item">
        <el-form-item label="教师姓名" :label-width="formLabelWidth">
          <el-select
            v-model="item.tecName"
            placeholder="专业教师"
            @change="jobNumChanged(item.tecName)"
          >
            <el-option
              v-for="teacher in teachers"
              :key="teacher.id"
              :label="teacher.tecName"
              :value="teacher.tecName"
            />

            <!-- key是唯一表标识，label是显示名，value是值 -->
          </el-select>
        </el-form-item>
        <el-form-item label="教师工号" :label-width="formLabelWidth">
          <el-input :disabled="true" v-model="item.jobNum" autocomplete="off" style="width:300px"></el-input>
        </el-form-item>

        <el-form-item label="上课地址" :label-width="formLabelWidth">
          <el-select v-model="item.roomAddr" placeholder="上课地址">
            <el-option
              v-for="room in rooms"
              :key="room.id"
              :label="room.roomAddr"
              :value="room.roomAddr"
            />

            <!-- key是唯一表标识，label是显示名，value是值 -->
          </el-select>
        </el-form-item>

        <el-form-item label="上课周" :label-width="formLabelWidth">
          <el-select v-model="item.courseWeek" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="上课时间" :label-width="formLabelWidth">
          <!-- <el-time-picker v-model="item.courseTime" placeholder="任意时间点"></el-time-picker> -->

          <el-time-select
            v-model="item.courseTime"
            :picker-options="{
              start: '08:00',
              step: '02:00',
              end: '22:00'
            }"
            placeholder="选择时间"
          ></el-time-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogForm = false">取 消</el-button>
        <el-button type="primary" @click="saveCourseManage">确 定</el-button>
      </div>
    </el-dialog>

    <!-- -------------------------课程安排---------------------------------- -->

    <!-- 分页 -->
    <el-pagination
      :current-page="page"
      :page-size="limit"
      :total="total"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="getList"
    />
    <!-- 这里  @current-change="getList" 不用传页数，饿了么内部封装了，只需要调方法即可。这个方法加括号不行，分页换显示不了-->
  </div>
</template>


<script>
import org from "@/api/organization/organization.js"; //.js可以省略
import stu from "@/api/personnel/student";
import course from "@/api/course/course";

export default {
  //2、写核心代码位置
  //data两种写法：
  // data:{
  // },
  data() {
    //定义数据变量和初始值
    return {
      //这里属性名可以自己起
      list: null, //查询之后接口返回集合
      total: 0,
      page: 1, //当前页
      limit: 5, //每页显示记录数
      studentDto: {}, //条件封装对象
      item: {},
      dialogFormVisible: false,
      departmentList: {},
      oneLevelList: {},
      twoLevelList: {},
      teacherDto: {},
      sysClassinfos: {}, //所有专业下的班级
      courseDto: {},
      teachers: {},
      dialogForm: false,
      // value1: "",
      options: [
        {
          value: "星期一",
          label: "星期一"
        },
        {
          value: "星期二",
          label: "星期二"
        },
        {
          value: "星期三",
          label: "星期三"
        },
        {
          value: "星期四",
          label: "星期四"
        },
        {
          value: "星期五",
          label: "星期五"
        },
        {
          value: "星期六",
          label: "星期六"
        },
        {
          value: "星期日",
          label: "星期日"
        }
      ],
      sysCourse: {},
      rooms: {}
    };
  },
  created() {
    this.getList();
    this.getClassLevel();
    this.getAllRoom();
  },
  methods: {
    getList(page = 1) {
      this.page = page;
      course
        .pageCourse(this.page, this.limit, this.courseDto)
        .then(response => {
          //response接口返回的数据
          // console.log(response)
          //6、打印返回的测试数据
          this.list = response.data.records; //records是后端接口返回的值
          this.total = response.data.total;
          //   console.log(this.list);
          //   console.log(this.total);
        }) //请求成功
        .catch(error => {
          console.log(error);
        }); //请求失败
    },
    resetData() {
      //清空的方法
      this.twoLevelList = {}
      //表单输入项数据清空
      this.courseDto = {};
      //查询度所有的表单数据
      this.getList();
    },

    //获取模糊查询的一二级分类
    getClassLevel() {
      org
        .getClassLevel()
        .then(response => {
          this.oneLevelList = response.data.oneLevelList;
        })
        .catch(error => {
          console.log(error);
        }); //请求失败
    },

    //二级专业跟随一级学院变化
    oneLevelChanged(value) {
      this.courseDto.professionalName = ''
      for (var i = 0; i < this.oneLevelList.length; i++) {
        var oneLevel = this.oneLevelList[i];
        // this.teacherDto.professionalName = ''
        if (value === oneLevel.departmentName) {
          this.twoLevelList = oneLevel.children;
          delete this.courseDto.professionalName;
          delete this.item.professionalName;
          delete this.item.classCode;
        }
      }
      
    },
    twoLevelChanged() {
      delete this.item.classCode;
      org
        .getClassCode(this.item.hiredate, this.item.professionalName)
        .then(response => {
          this.sysClassinfos = response.data.sysClassinfos;
        });
    },

    //新增班级
    saveCourseInfo() {
      course.addCourse(this.item).then(response => {
        //添加成功
        //提示信息
        this.$message({
          type: "success",
          message: "添加成功!"
        });
        this.dialogFormVisible = false;
        this.getList();
      });
    },

    //查询单个课程详情
    getCourseById(id) {
      course
        .getCourseById(id)
        .then(response => {
          this.item = response.data.item;
          this.dialogFormVisible = true;
        }) //请求成功
        .catch(error => {
          console.log(error);
        }); //请求失败
    },
    //新增专业清空表单
    saveClear() {
      this.item = {};
      this.dialogFormVisible = true;
      this.courseDto = {};
      this.twoLevelList = {}
    },
    //新增或修改课程信息
    saveOrUpdateCourseInfo() {
      if (!this.item.id) {
        this.saveCourseInfo();
      } else {
        this.updateCourse();
      }
    },

    //修改课程信息
    updateCourse() {
      course
        .updateCourse(this.item)
        .then(response => {
          this.item = {};
          this.dialogFormVisible = false;
          this.getList();
          this.$message({
            type: "success",
            message: "修改成功!"
          });
        }) //请求成功
        .catch(error => {
          console.log(error);
        });
    },
    //删除用户
    removeDataById(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        course
          .delCourse(id)
          .then(response => {
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.getList();
          }) //请求成功
          .catch(error => {
            console.log(error);
          });
      });
    },
    //课程安排
    saveOrUpdateCourse(id, professionalName, tecName) {
      console.log(tecName);
      console.log(id);
      course.getCourseById(id).then(response => {
        this.item = response.data.item;
      });
      // if(!id){//第一次加老师

      this.selectTeacher(professionalName);
      this.dialogForm = true;
      // }else{

      // this.selectTeacher(professionalName)
      // this.dialogForm = true;
      // }
    },
    //根据专业名获取所属所有讲师
    selectTeacher(professionalName) {
      org
        .getTeacherByProfessionalName(professionalName)
        .then(response => {
          this.teachers = response.data.teachers;
        }) //请求成功
        .catch(error => {
          console.log(error);
        });
    },
    //选择教师工号显示
    jobNumChanged(tecName) {
      console.log(tecName);
      this.$forceUpdate();
      console.log(this.item.id);

      // this.teacher.tecName = tecName;
      for (var i = 0; i < this.teachers.length; i++) {
        var teacher = this.teachers[i];
        if (teacher.tecName === tecName) {
          this.item.jobNum = teacher.jobNum;
        }
      }
    },
    //查询所有教室
    getAllRoom() {
      org.getAllRoom().then(response => {
        this.rooms = response.data.rooms;
      });
    },
    //保存课程分配安排
    saveCourseManage() {
      console.log(this.item.courseTime)
      course
        .updateCourseManage(this.item)
        .then(response => {
          this.$message({
            type: "success",
            message: "修改成功!"
          });
          this.dialogForm = false
          this.getList();
        }) //请求成功
        .catch(error => {
          console.log(error);
        });
    }
  }
};
</script>